/**
 * Created by midori on 2015/12/7.
 *
 * This class is used to handle the process of the Restful http call.
 */
(function(){

    var  __bind = function(fn,me){return function(){return fn.apply(me,arguments);};};

    var RestAngularHandler = (function(){

        function RestAngularHandler(scope){
            // scope contains the variable which displays the status of the http call.
            this.scope = scope;
            this.getErrorCallback = __bind(this.getErrorCallback, this);
            this.getSuccessCallback = __bind(this.getSuccessCallback, this);
            this.stop = __bind(this.stop, this);
            this.start = __bind(this.start, this);
            this.scope.isSending = false;
            this.scope.isError = false;
            if (this.scope.errors == null) {
                this.scope.errors = [];
            }
        }

        RestAngularHandler.prototype.start = function(){
            this.scope.isSending = true;
            return this;
        };

        RestAngularHandler.prototype.stop = function() {
            this.scope.isSending = false;
            return this;
        };
        /**
         * result is the parameter returned from the promise of RestAngular for exmample:
         * restHandler = restAngularHandlerFactory.createAndStart(scope.rest.startTestRun);
         * restangular.one("test",testId).post().then(restHandler.getSuccessCallback());
         */

        RestAngularHandler.prototype.getSuccessCallback = function(handler) {
            var myScope;
            if (handler == null) {
                handler = function(result) {};
            }
            myScope = this.scope;
            return function(result) {
                myScope.isSending = false;
                return handler(result);
            };
        };

        RestAngularHandler.prototype.getErrorCallback = function(handler) {
            var myScope;
            if (handler == null) {
                handler = function(errorResponse) {};
            }
            myScope = this.scope;
            return function(errorResponse) {
                var error;
                myScope.isSending = false;
                myScope.isError = true;
                error = {};
                if (errorResponse.status === 0) {
                    error.isTimeout = true;
                } else {
                    error.isTimeout = false;
                    error.status = errorResponse.status;
                    error.code = errorResponse.data.code;
                    error.message = errorResponse.data.message;
                }
                myScope.errors.push(error);
                return handler(errorResponse);
            };
        };

        return RestAngularHandler;

    })();


    service = angular.module("services.utils.RestAngularHandler", []);

    service.factory("RestAngularHandlerFactory", function(){
        return {
            create: function(scope) {
                return new RestAngularHandler(scope);
            },
            createAndStart: function(scope) {
                return (new RestAngularHandler(scope)).start();
            }
        };
    });

}).call(this);
